<template>
	<view class="release-basic">
		<view class="basic-info">
			<text>基本信息</text>
			<text class="iconfont icon-close" @click="close()"></text>
		</view>
		<view class="release-list">
			<text>项目名称：</text>
			<input maxlength="12" type="text" value="" v-model="name" placeholder="必须真实项目名,否则审核不过" />
		</view>
		<view class="release-list">
			<text>悬赏标题：</text>
			<input maxlength="30" type="text" value="" v-model="title" placeholder="设置一个悬赏标题" />
		</view>
		<view class="release-list">
			<text>提交时间：</text>
			<view class="limit-time" @click="handleLimitShow(1)">
				<text>限制{{submit_time < 24 ? submit_time+'小时':submit_time/24+'天'}}提交</text>
				<text class="iconfont icon-dayu"></text>
			</view>
		</view>
		<view class="release-list">
			<text>审核时间：</text>
			<view class="limit-time" @click="handleLimitShow(2)">
				<view class="limit-toast">
					<text>将在{{examine_time<24 ? examine_time+'小时':examine_time/24+'天'}}内审核</text>
					<text>若未按时审核，将自动通过！</text>
				</view>
				<text class="iconfont icon-dayu"></text>
			</view>
		</view>
		<view class="release-list">
			<text>重复任务：</text>
			<view class="radio-group">
				<radio-group class="radio-group-lists" @change="radioChange">
					<label class="radio-group-list" v-for="(item, index) in radioGroup" :key="item.value">
						<radio :color="'#333333'" :value="item.value" :checked="item.value === num" />
						<view>{{item.name}}</view>
					</label>
				</radio-group>
			</view>
		</view>
		<view class="release-list">
			<text>悬赏单价：</text>
			<input type="number" :style="{color:idDisabled?'#888888':''}" :disabled="idDisabled" value="" v-model="price" :placeholder="minPrice+ ' zkb / 人'" />
		</view>
		<view class="release-list">
			<text>悬赏名额：</text>
			<input type="number" :style="{color:idDisabled?'#888888':''}" :disabled="idDisabled" value="" v-model="quota" :placeholder="'最少 '+ minQuota +' 人'" />
		</view>
		<view class="release-list">
			<text>预付赏金：</text>
			<view class="advance-reward">
				<view class="advance-reward-num">
					<text>共计</text>
					<text>{{Number(price*quota).toFixed(2)}}</text>
					<text>zkb</text>
				</view>
				<view class="advance-reward-toast">
					服务费 {{procedures}}%
				</view>
			</view>
		</view>
		<view class="next-step">
			<button type="default" @click="nextBtn()">下一步（设置步骤）</button>
		</view>
		<uni-popup ref="limitPopup" type="bottom">
			<view class="limit-popup">
				<view class="limit-tiast">
					选择时间（小时，天）
				</view>
				<view class="limit-lists">
					<view class="limit-list" v-for="(item,index) of limitTime" :key="index"
						@click="handleLimitSelect(item)">
						<text>{{item.value<24 ? item.value+'小时':item.value/24+'天'}}</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {isEmpty} from "@/utils/util.js"
	import {classification,rewardGet} from "@/api/reward.js"
	export default{
		name:"release-task",
		data(){
			return{
				procedures:15, //手续费
				minPrice:1, //最小金额
				minQuota:10,//最小名额
				titleTabar:"",//标题
				radioGroup:[
					{
						value:'1',
						name:"每天1次"
					},
					{
						value:'2',
						name:"只能1次",
						checked: 'true'
					}
				],
				num:'2',
				limitTimeType:1, //时间类型
				limitTime:[
					{
						value:3,
					},
					{
						value:24,
					},
					{
						value:72,
					},
					{
						value:120,
					},
					{
						value:168,
					}
				],
				submit_time:3, //提交类型
				examine_time:3, //验收时间
				//项目
				name:"", //名称
				title:"", //标题
				price:"", //单价
				quota:"", //名额
				type:"1",
				//编辑 区分
				reward_id:0,
				idDisabled:false
			}
		},
		async onLoad(option) {
			//编辑悬赏
			if(option.reward_id){
				this.reward_id = option.reward_id;
				this.titleTabar = "编辑悬赏";
				this.idDisabled = true;
				let res = await rewardGet({id:option.reward_id});
				if(res.code === 1){
					this.charge(res.data.cid);
					this.edit(res.data);
				}else{
					this.$msg("服务器异常！");
					setTimeout(()=>{
					  uni.navigateBack()	
					},1000)
				}
			}else{
				this.titleTabar = option.titleTabar
				this.cid = option.id;
				//手续费
				this.charge(option.id);
			}
		},
		methods:{
			//手续费
			charge(id){
				classification({id:id}).then(res=>{
					if(res.code === 1){
						this.procedures = res.data.charge;
						this.minPrice = res.data.min_price;
						this.minQuota = res.data.min_quota;
						this.type = res.data.type;
					}
				})
			},
			radioChange(evt){
				this.num = evt.detail.value;
			},
			handleLimitShow(type){
				this.limitTimeType = type
				this.$refs.limitPopup.open() 
			},
			handleLimitSelect(item){
				if(this.limitTimeType === 1){
					this.submit_time = item.value
				}
				if(this.limitTimeType === 2){
					this.examine_time = item.value
				}
				this.$refs.limitPopup.close()
			},
			//下一步
			nextBtn(){
				let result = isEmpty([{
						type: '项目名称',
						value: this.name
					},
					{
						type: '悬赏标题',
						value: this.title
					},
					{
						type: '悬赏单价',
						value: this.price
					},
					{
						type: '悬赏名额',
						value: this.quota
					}
				]);
				if(!result.success){
					this.$msg(result.msg)
					return;
				}
				if(this.price<this.minPrice){
					this.$msg("最小单价为"+this.minPrice+"zkb")
					return;
				}
				if(this.quota<this.minQuota){
					this.$msg("最少名额为"+this.minQuota+"人")
					return;
				}
				let obj = {
					cid:this.cid, 
					titleTabar:this.titleTabar,//页面标题
					name:this.name, //名称
					title:this.title, //标题
					price:this.price, //单价
					quota:this.quota, //名额
					submit_time:this.submit_time, //提交时间
					examine_time:this.examine_time,//验收时间
					num:this.num ,//任务类型
					type:this.type,
					reward_id: this.reward_id
				}
				uni.navigateTo({
					url:"/pages/mutual/release-success/release-success?basic="+encodeURI(JSON.stringify(obj))
				})
			},
			//编辑信息
			edit(data){
				this.name = data.name;
				this.title = data.title;
				this.price = data.price+Math.floor(data.price+(this.procedures/100) * 100) / 100;
				this.quota = data.quota;
				this.type = data.type;
				this.submit_time = data.submit_time;
				this.examine_time = data.examine_time;
				this.cid = data.cid;
			},
			//清除
			close(){
				this.name = ""
				this.title = ""
				this.price = ""
				this.quota = ""
				this.num = "2"
				this.limitTimeType = 1
				this.submit_time=3
				this.examine_time=3
				this.radioGroup[0].checked = "false"
				this.radioGroup[1].checked = "true"
			},
		},
		watch:{
			titleTabar:{
				handler(newValue){
					if(newValue){
						uni.setNavigationBarTitle({
							title:newValue
						})
					}
				},
				immediate: true
			}
		}
	}
</script>

<style lang="less" scoped src="./release-next.less">
</style>
